package com.windfone.web.action;

import java.util.Date;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import com.windfone.common.Constant;
import com.windfone.common.PageUtil;
import com.windfone.model.Software;
import com.windfone.model.SoftwareType;
import com.windfone.service.SoftwareService;

public class SoftwareAction extends ActionSupport {

	/**	serialVersionUID */
	private static final long serialVersionUID = 1L;
	private SoftwareService softwareService;
	private List<SoftwareType> softwareTypes;
	
	public SoftwareService getSoftwareService() {
		return softwareService;
	}

	public String getName() {
		return name;
	}

	public String getVersion() {
		return version;
	}

	public String getSystem() {
		return system;
	}

	public String getSmallImagepath() {
		return smallImagepath;
	}

	public String getBigImagepath() {
		return bigImagepath;
	}

	public String getLastUpdate() {
		return lastUpdate;
	}

	public int getSoftwareTypeId() {
		return softwareTypeId;
	}

	public String getKeyword() {
		return keyword;
	}

	public int getDownCount() {
		return downCount;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public String getFileName() {
		return fileName;
	}

	public Software getBean() {
		return bean;
	}

	public int getBeginRecord() {
		return beginRecord;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public int getNextPage() {
		return nextPage;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public int getPrevPage() {
		return prevPage;
	}

	public int getSoftwareId() {
		return softwareId;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setVersion(String version) {
		this.version = version;
	}

	public void setSystem(String system) {
		this.system = system;
	}

	public void setSmallImagepath(String smallImagepath) {
		this.smallImagepath = smallImagepath;
	}

	public void setBigImagepath(String bigImagepath) {
		this.bigImagepath = bigImagepath;
	}

	public void setLastUpdate(String lastUpdate) {
		this.lastUpdate = lastUpdate;
	}

	public void setSoftwareTypeId(int softwareTypeId) {
		this.softwareTypeId = softwareTypeId;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}

	public void setDownCount(int downCount) {
		this.downCount = downCount;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public void setBean(Software bean) {
		this.bean = bean;
	}

	public void setBeginRecord(int beginRecord) {
		this.beginRecord = beginRecord;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public void setNextPage(int nextPage) {
		this.nextPage = nextPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public void setPrevPage(int prevPage) {
		this.prevPage = prevPage;
	}

	public void setSoftwareId(int softwareId) {
		this.softwareId = softwareId;
	}

	private String name;
	private String version;
	private String system;
	private String smallImagepath;
	private String bigImagepath;
	private String lastUpdate;
	private int softwareTypeId;
	private String keyword;
	private int downCount;
	private Date createTime;
	private String fileName;
	private int langId;
	private String content;
	private Software bean;
	private int beginRecord;
	private int currentPage;
	private int nextPage;
	private int totalPage;
	private int prevPage;
	private int softwareId;
	
	private List<Software> softwareList;
	public List<Software> getSoftwareList() {
		return softwareList;
	}
	
	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}
	public int getLangId() {
		return langId;
	}

	public void setLangId(int langId) {
		this.langId = langId;
	}

	public void setSoftwareList(List<Software> softwareList) {
		this.softwareList = softwareList;
	}

	public List<SoftwareType> getSoftwareTypes() {
		return softwareTypes;
	}

	public void setSoftwareTypes(List<SoftwareType> softwareTypes) {
		this.softwareTypes = softwareTypes;
	}

	public void setSoftwareService(SoftwareService softwareService) {
		this.softwareService = softwareService;
	}
	
	public String execute() throws Exception {
		softwareTypes = softwareService.getAllSoftwareType();
		return SUCCESS;
	}
	
	public String beforUpdateSoftware() throws Exception {
		softwareTypes = softwareService.getAllSoftwareType();
		bean = softwareService.getSoftwareById(softwareId).get(0);
		return SUCCESS;
	}
	
	public String deleteSoftware() throws Exception {
		bean = new Software();
		bean.setId(softwareId);
		softwareService.deleSoftware(bean);
		return SUCCESS;
	}
	
	public String addSoftware() throws Exception {
		Software bean = new Software();
		bean.setBigImagepath(bigImagepath);
//		bean.setCreateTime(createTime);
		bean.setDownCount(downCount);
		bean.setFileName(fileName);
		bean.setKeyword(keyword);
		bean.setLangId(langId);
		bean.setLastUpdate(lastUpdate);
		bean.setName(name);
		bean.setSmallImagepath(smallImagepath);
		bean.setSystem(system);
		bean.setSoftwareTypeId(softwareTypeId);
		bean.setVersion(version);
		bean.setContent(content);
		softwareService.addSoftware(bean);
		return SUCCESS;
	}
	
 
	public String updateSoftware() throws Exception {
		Software bean = new Software();
		bean.setBigImagepath(bigImagepath);
		bean.setId(softwareId);
		bean.setDownCount(downCount);
		bean.setFileName(fileName);
		bean.setKeyword(keyword);
		bean.setLangId(langId);
		bean.setLastUpdate(lastUpdate);
		bean.setName(name);
		bean.setSmallImagepath(smallImagepath);
		bean.setSystem(system);
		bean.setSoftwareTypeId(softwareTypeId);
		bean.setVersion(version);
		bean.setContent(content);
		softwareService.updateSoftwareById(bean);
		return SUCCESS;
	}
	
	public String getBeginDate() {
		return beginDate;
	}

	public String getEndDate() {
		return endDate;
	}

	public int getSoftwareTypeIdQ() {
		return softwareTypeIdQ;
	}

	public void setBeginDate(String beginDate) {
		this.beginDate = beginDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	public void setSoftwareTypeIdQ(int softwareTypeIdQ) {
		this.softwareTypeIdQ = softwareTypeIdQ;
	}

	private String beginDate;
	private String endDate;
	private int softwareTypeIdQ = -1;
	public String softwareList() throws Exception {
		softwareTypes = softwareService.getAllSoftwareType();
		String whereCondition = "";
		if ( softwareTypeIdQ !=-1 ){
			whereCondition = " AND s.softwareTypeId="+ softwareTypeIdQ;
		}
		if ( beginDate!=null && !beginDate.equals("") ){
			whereCondition = whereCondition + "  AND s.createTime>='"+beginDate +"' ";
		}
		if ( endDate!=null && !endDate.equals("") ){
			whereCondition = whereCondition + "  AND s.createTime<='"+ endDate  +"'  ";
		}
		PageUtil pu = new PageUtil();
		pu.setCurrentPage(currentPage);
		int totalRecords = softwareService.getAllRecorderNumber(whereCondition) ;
		pu.setTotalRecords( totalRecords );
		pu.setPageSize(Constant.PAGE_SIZE);
		pu.dealPage();
		currentPage = pu.getCurrentPage();
		nextPage = pu.getNextPage();
		totalPage = pu.getTotalPage();
		prevPage = pu.getPrevPage();
		
		beginRecord = Constant.PAGE_SIZE * (currentPage -1);
		if ( beginRecord< 0 )
			beginRecord = 0; 
		softwareList = softwareService.getAllSoftware(beginRecord,whereCondition);		
		return SUCCESS;
	}

}
